package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.dbmodel.ModelMain;
import cn.com.yx.ms.domain.dbmodel.StlMain;
import cn.com.yx.ms.domain.dbmodel.SysRole;
import cn.com.yx.ms.domain.release.ReleaseModel;
import cn.com.yx.ms.domain.stl.StlJmMapModel;
import cn.com.yx.ms.domain.stl.StlJmModel;
import cn.com.yx.ms.domain.stl.StlMainModel;
import cn.com.yx.ms.domain.stl.StlScModel;
import cn.com.yx.ms.service.IModelService;
import cn.com.yx.ms.service.IReleaseService;
import cn.yx.framework.configuration.ApiCode;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.util.MsgPropertyUtil;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 发布管理
 */
@RestController
@RequestMapping(value = "release")
@Api(value = "发布管理", description = "发布式样节目发布")
public class ReleaseController {
    @Resource
    private IReleaseService iReleaseService;
    @Resource
    private IModelService iModelService;

    @ResponseBody
    @RequestMapping(value = "jm/getAllJmMain", method = RequestMethod.POST)
    @ApiOperation("获取/查询节目发布")
    public ApiResult findCatalogs(@RequestBody @Validated StlJmModel stlJmModel) {
        return iReleaseService.getAllJmMain(stlJmModel);
    }

    @ResponseBody
    // @RequestMapping(value = "jm/getAllJmByControlid/{modelid}/{controlid}/{regioncode}", method = RequestMethod.GET)
    @RequestMapping(value = "jm/getAllJmByControlid/{controlid}", method = RequestMethod.GET)
    @ApiOperation("根据栏目id获取节目列表")
    public ApiResult getAllJmByControlid(@PathVariable("controlid") String controlid,
                                         @ApiQuery ApiQueryBase query,
                                         HttpServletRequest request) {
        query.getConditions().put("programname", request.getParameter("programname"));
        return iReleaseService.getAllJmByControlid(controlid, query);
    }

    @ResponseBody
    @RequestMapping(value = "jm/getDevByStyleid/{styleid}", method = RequestMethod.GET)
    @ApiOperation("根据式样id获取设备列表")
    public ApiResult getDevByStyleid(@PathVariable("styleid") String styleid) {
        return iReleaseService.getDevByStyleid(styleid);
    }

    @PostMapping("jm/addJmMain")
    @ApiOperation(value = "新增节目", notes = "新增节目")
    @ApiImplicitParam(paramType = "body", name = "stlMainModel", dataType = "Map", required = true, value = "新增节目")
    public ApiResult addstlmain(@RequestBody @Validated StlScModel stlMainModel) {
        return iReleaseService.addJmMain(stlMainModel);
    }

    @PostMapping("jm/updateJmMain")
    @ApiOperation(value = "更新节目", notes = "更新节目")
    @ApiImplicitParam(paramType = "body", name = "stlMainModel", dataType = "Map", required = true, value = "更新节目")
    public ApiResult updatestlmain(@RequestBody @Validated StlJmMapModel stlJmMapModel) {
        return iReleaseService.updateJmMain(stlJmMapModel);
    }

    @ResponseBody
    @RequestMapping(value = "jm/getScByJmid/{jmid}", method = RequestMethod.GET)
    @ApiOperation("根据节目id获取素材列表")
    public ApiResult getScByJmid(@PathVariable("jmid") String jmid) {
        return iReleaseService.getScByJmid(jmid);
    }

    @ResponseBody
    @RequestMapping(value = "stl/catalogs/{ps}/{pi}", method = RequestMethod.POST)
    @ApiOperation("获取/查询式样发布")
    public ApiResult findStlCatalogs(@RequestBody @Validated StlMain stlMain, @PathVariable("ps") String ps, @PathVariable("pi") String pi) {
        // 查询条件接受
        ApiQueryBase query = new ApiQueryBase();
        query.setPi(Integer.parseInt(pi));
        query.setPs(Integer.parseInt(ps));
        query.getConditions().put("ps", ps);
        query.getConditions().put("pi", pi);
        query.getConditions().put("guid", stlMain.getGuid());
        query.getConditions().put("name", stlMain.getName());

        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<StlMainModel> page = (Page<StlMainModel>) iReleaseService.findStlMain(query);
        ApiPager<StlMainModel> apiPager = new ApiPager<StlMainModel>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);

    }

    @PostMapping(value = "stl/delStlMain")
    @ApiOperation("删除式样")
    @ApiImplicitParam(paramType = "path", name = "guids", dataType = "String", required = true, value = "式样id")
    public ApiResult delStlMain(@RequestBody @Validated StlMain stlMain) {
        // 删除式样
        ApiResult apiResult = iReleaseService.removeStlMain(stlMain.getGuid());
        return apiResult;
    }

    @PostMapping("stl/addstlmain")
    @ApiOperation(value = "创建式样信息", notes = "创建式样信息")
    @ApiImplicitParam(paramType = "body", name = "stlMainModel", dataType = "Map", required = true, value = "式样信息")
    public ApiResult addstlmain(@RequestBody @Validated StlMainModel stlMainModel) {
        return iReleaseService.addStlMain(stlMainModel);
    }

    @PostMapping("stl/updatestlmain")
    @ApiOperation(value = "更新式样信息", notes = "更新式样信息")
    @ApiImplicitParam(paramType = "body", name = "stlMainModel", dataType = "Map", required = true, value = "式样信息")
    public ApiResult updatestlmain(@RequestBody @Validated StlMainModel stlMainModel) {
        return iReleaseService.updateStlMain(stlMainModel);
    }

    @ResponseBody
    @RequestMapping(value = "stl/getAllModelMain", method = RequestMethod.POST)
    @ApiOperation("根据模板id获取/查询模板一览")
    public ApiResult getAllModelMain(@RequestBody @Validated ModelMain modelModel) {
        return iReleaseService.getAllModelMain(modelModel.getGuid());
    }

    @ResponseBody
    @RequestMapping(value = "stl/getOrgList", method = RequestMethod.GET)
    @ApiOperation("获取所有管理授权单位一览")
    public ApiResult getOrgList() {
        return iReleaseService.getOrgList();
    }

    @ResponseBody
    @RequestMapping(value = "jm/getJmByStyleid/{styleid}", method = RequestMethod.GET)
    @ApiOperation("设备同步-加载")
    public ApiResult getJmByStyleid(@PathVariable("styleid") String styleid) {
        return iReleaseService.getJmByStyleid(styleid);
    }

    @ResponseBody
    @RequestMapping(value = "stl/getAllStyles", method = RequestMethod.GET)
    @ApiOperation("设备同步选择式样")
    public ApiResult getAllStyles() {
        return iReleaseService.getAllStyles();
    }

    @ResponseBody
    @RequestMapping(value = "stl/getAllJmByStyleid/{styleid}", method = RequestMethod.GET)
    @ApiOperation("根据式样id获取式样详细信息")
    public ApiResult getAllJmByStyleid(@PathVariable("styleid") String styleid) {
        return iReleaseService.getAllJmByStyleid(styleid);
    }

    @ResponseBody
    @RequestMapping(value = "style/default/models", method = RequestMethod.GET)
    @ApiOperation("获取默认式样下的模板")
    public ApiResult getDefaultStyleModels() {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iModelService.getDefaultStyleModels());
    }

    // region << 发布节目 >>

    @PostMapping("")
    @ApiOperation(value = "发布式样", notes = "发布式样")
    @ApiImplicitParam(paramType = "body", name = "stlMainModel", dataType = "Map", required = true, value = "式样信息")
    public ApiResult releaseProgram(@RequestBody @Validated ReleaseModel releaseModel) throws Exception {
        ApiCode apiCode = iReleaseService.releaseProgram(releaseModel, true);
        return new ApiResult(AppConstant.MSG_SUCCESS,
                apiCode == ApiCode.APP_100200, MsgPropertyUtil.getProperty(apiCode.msgId),
                null);
    }

    @ResponseBody
    @RequestMapping(value = "styles/page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的式样列表")
    public ApiResult findReleaseStyle(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

//        // 查询条件接受
//        query.getConditions().put("ps", request.getParameter("ps"));
//        query.getConditions().put("pi", request.getParameter("pi"));
//        query.getConditions().put("stylename", request.getParameter("stylename"));
//
//
//        // 翻页对象构建
//        PageHelper.startPage(query.getPi(), query.getPs(), true);
//        Page<ReleaseStyleModel> page = (Page<ReleaseStyleModel>) iReleaseService.findReleaseStyles(query);
//        ApiPager<ReleaseStyleModel> apiPager = new ApiPager<>(query.getPs(),
//                query.getPi(),
//                page.getTotal(),
//                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iReleaseService.findPageReleaseStyles(query, request));

    }

    // endregion
}
